﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class EditRestaurant : AdminPageBase
    {
        private RestaurantInfo _restaurantInfo = new RestaurantInfo();
        private readonly RestaurantController _restaurantController = new RestaurantController();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["RestaurantId"]))
                _restaurantInfo = _restaurantController.GetRestaurant(int.Parse(Request.QueryString["RestaurantId"]));

            if (Page.IsPostBack) return;

            LoadPartner();
            LoadAddressDefault();
            LoadContinent();
            LoadAdminUser();
            LoadStatus();
            LoadBuiltYear();
            LoadRestaurantType();

            if (_restaurantInfo.RestaurantId == -1) return;

            LoadNational();
            LoadCity();
            LoadDistrict();
            LoadWard();

            RenderForm();

        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Pages/ListRestaurant.aspx?xml=Restaurant");
        }

        private void SaveData()
        {
            _restaurantInfo.RestaurantFullName = txtRestaurantFullName.Text;
            _restaurantInfo.RestaurantSortName = txtRestaurantSortName.Text;
            _restaurantInfo.RestaurantEnglishName = txtRestaurantEnglishName.Text;
            _restaurantInfo.DetailDescription = txtContent.Text;
            _restaurantInfo.PartnerId = int.Parse(ddlPartner.SelectedValue);
            _restaurantInfo.RestaurantTypeId = int.Parse(ddlRestaurantType.SelectedValue);
            _restaurantInfo.BuiltYear = int.Parse(ddlBuiltYear.SelectedValue);
            _restaurantInfo.ContinentId = int.Parse(ddlContinent.SelectedValue);
            _restaurantInfo.NationalId = int.Parse(Request["ctl00$placeHolderContent$ddlNational"]);
            _restaurantInfo.CityId = int.Parse(Request["ctl00$placeHolderContent$ddlCity"]);
            _restaurantInfo.DistrictId = int.Parse(Request["ctl00$placeHolderContent$ddlDistrict"]);
            _restaurantInfo.WardId = int.Parse(Request["ctl00$placeHolderContent$ddlWard"]);
            _restaurantInfo.Address = txtAddress.Text;
            _restaurantInfo.RepresentName = txtRepresentName.Text;
            _restaurantInfo.RepresentPhone = txtRepresentPhone.Text;
            _restaurantInfo.RepresentHotline = txtRepresentHotline.Text;
            _restaurantInfo.RepresentFax = txtRepresentFax.Text;
            _restaurantInfo.RepresentEmail = txtRepresentEmail.Text;
            _restaurantInfo.RepresentWebsite = txtRepresentWebsite.Text;
            _restaurantInfo.SortDescription = txtSortDescription.Text;
            _restaurantInfo.Status = int.Parse(ddlStatus.SelectedValue);

            if (_restaurantInfo.RestaurantId == -1)
            {
                _restaurantInfo.CreatedBy = ddlAdminUser.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlAdminUser.SelectedValue);
                _restaurantInfo.CreatedDate = DateTime.Now;
                _restaurantController.InsertRestaurant(_restaurantInfo);
            }
            else
            {
                _restaurantInfo.UpdatedBy = UserId;
                _restaurantInfo.UpdatedDate = DateTime.Now;
                _restaurantController.UpdateRestaurant(_restaurantInfo);
            }

            Response.Redirect("/Pages/ListRestaurant.aspx?xml=Restaurant");
        }

        private void RenderForm()
        {
            txtRestaurantFullName.Text = _restaurantInfo.RestaurantFullName;
            txtRestaurantSortName.Text = _restaurantInfo.RestaurantSortName;
            txtRestaurantEnglishName.Text = _restaurantInfo.RestaurantEnglishName;
            txtContent.Text = _restaurantInfo.DetailDescription;
            ddlPartner.SelectedValue = _restaurantInfo.PartnerId.ToString();
            ddlBuiltYear.SelectedValue = _restaurantInfo.BuiltYear.ToString();
            ddlContinent.SelectedValue = _restaurantInfo.ContinentId.ToString();
            ddlNational.SelectedValue = _restaurantInfo.NationalId.ToString();
            ddlCity.SelectedValue = _restaurantInfo.CityId.ToString();
            ddlDistrict.SelectedValue = _restaurantInfo.DistrictId.ToString();
            ddlWard.SelectedValue = _restaurantInfo.WardId.ToString();
            txtAddress.Text = _restaurantInfo.Address;
            txtRepresentName.Text = _restaurantInfo.RepresentName;
            txtRepresentPhone.Text = _restaurantInfo.RepresentPhone;
            txtRepresentHotline.Text = _restaurantInfo.RepresentHotline;
            txtRepresentFax.Text = _restaurantInfo.RepresentFax;
            txtRepresentEmail.Text = _restaurantInfo.RepresentEmail;
            txtRepresentWebsite.Text = _restaurantInfo.RepresentWebsite;
            txtSortDescription.Text = _restaurantInfo.SortDescription;
            ddlAdminUser.SelectedValue = _restaurantInfo.CreatedBy.ToString();
            ddlStatus.SelectedValue = _restaurantInfo.Status.ToString();
        }

        private void LoadRestaurantType()
        {
            ddlRestaurantType.DataSource = new RestaurantTypeController().ListRestaurantType()
                .Where(p => p.Status == (int)Globals.DocumentStatus.Use)
                .OrderBy(p => p.RestaurantTypeName).ToList();
            ddlRestaurantType.DataTextField = "RestaurantTypeName";
            ddlRestaurantType.DataValueField = "RestaurantTypeId";
            ddlRestaurantType.DataBind();

            ddlRestaurantType.Items.Insert(0, new ListItem("--- Chọn loại nhà hàng ---", "-1"));
        }
        private void LoadBuiltYear()
        {
            for (var i = DateTime.Now.Year; i >= 1900; i--)
            {
                ddlBuiltYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            ddlBuiltYear.Items.Insert(0, new ListItem("--- Chọn năm xây dựng ---", "-1"));
        }
        private void LoadPartner()
        {
            ddlPartner.DataSource = new PartnerController().ListPartner()
                .Where(p => p.Status == (int)Globals.DocumentStatus.Use)
                .OrderBy(p => p.PartnerName).ToList();
            ddlPartner.DataTextField = "PartnerName";
            ddlPartner.DataValueField = "PartnerId";
            ddlPartner.DataBind();
        
            ddlPartner.Items.Insert(0, new ListItem("--- Chọn đối tác ---", "-1"));
        }
        private void LoadAddressDefault()
        {
            ddlNational.Items.Insert(0, new ListItem("--- Chọn quốc gia ---", "-1"));
            ddlCity.Items.Insert(0, new ListItem("--- Chọn tỉnh/thành phố ---", "-1"));
            ddlDistrict.Items.Insert(0, new ListItem("--- Chọn quận/huyện ---", "-1"));
            ddlWard.Items.Insert(0, new ListItem("--- Chọn phường/xã ---", "-1"));
        }
        private void LoadContinent()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Continent.xml"));
            var dv = ds.Tables["Continent"].DefaultView;
            ddlContinent.DataTextField = "Name";
            ddlContinent.DataValueField = "ID";
            ddlContinent.DataSource = dv;
            ddlContinent.DataBind();

            ddlContinent.Items.Insert(0, new ListItem("--- Chọn châu lục ---", "-1"));
        }
        private void LoadNational()
        {
            ddlNational.DataSource = new NationalController().ListNationalByContinent(_restaurantInfo.ContinentId);
            ddlNational.DataTextField = "NationalName";
            ddlNational.DataValueField = "NationalId";
            ddlNational.DataBind();

            ddlNational.Items.Insert(0, new ListItem("--- Chọn quốc gia ---", "-1"));
        }
        private void LoadCity()
        {
            ddlCity.DataSource = new CityController().ListCityByNational(_restaurantInfo.NationalId);
            ddlCity.DataTextField = "CityName";
            ddlCity.DataValueField = "CityId";
            ddlCity.DataBind();

            ddlCity.Items.Insert(0, new ListItem("--- Chọn tỉnh/thành phố ---", "-1"));
        }
        private void LoadDistrict()
        {
            ddlDistrict.DataSource = new DistrictController().ListDistrictByCity(_restaurantInfo.CityId);
            ddlDistrict.DataTextField = "DistrictName";
            ddlDistrict.DataValueField = "DistrictId";
            ddlDistrict.DataBind();

            ddlDistrict.Items.Insert(0, new ListItem("--- Chọn quận/huyện ---", "-1"));
        }
        private void LoadWard()
        {
            ddlWard.DataSource = new WardController().ListWardByDistrict(_restaurantInfo.DistrictId);
            ddlWard.DataTextField = "WardName";
            ddlWard.DataValueField = "WardId";
            ddlWard.DataBind();

            ddlWard.Items.Insert(0, new ListItem("--- Chọn phường/xã ---", "-1"));
        }
        private void LoadAdminUser()
        {
            ddlAdminUser.DataSource = new AdminUserController().ListAdminUser();
            ddlAdminUser.DataTextField = "Username";
            ddlAdminUser.DataValueField = "UserID";
            ddlAdminUser.DataBind();

            ddlAdminUser.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }
        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }
    }
}